<?php
class apibusiness_response_order_ccb_b2cv1 extends  apibusiness_response_order_ccb_abstract{
    
    /**
     * 是否接收(除活动订单外的其他订单)
     *
     * @return void
     * @author
     **/
    protected function accept_dead_order(){
        $rs = parent::accept_dead_order();
        #订单取消的，先放过(因为可能订单更新，即：前端已全额退款，需要在ERP接受后，做订单取消)
        if ($rs == false && $this->_ordersdf['status'] == 'dead') {
            unset($this->_apiLog['info']['msg']);
            return true;
        }
        return $rs;
    }
    /*     
     * 是否接收订单
    *
    * @return void
    * @author
    **/
    protected function canAccept(){
        $result = parent::canAccept();
        if ($result === false) {
            return false;
        }
        #未支付的款到发货订单拒收
        if ($this->_ordersdf['shipping']['is_cod'] != 'true' && $this->_ordersdf['pay_status'] == '0') {
            $this->_apiLog['info']['msg'] = '未支付订单不接收';
            return false;
        }
        return true;
    }    
            
    /**
     * 
     *
     * @return void
     * @author
     **/
    public function canCreate(){
        if ($this->_ordersdf['status'] != 'active') {
            $this->_apiLog['info']['msg'] = ($this->_ordersdf['status'] == 'dead') ? '取消的订单不接收' : '完成的订单不接收';
            return false;
        }
        #建行创建订单的时候，未支付订单不接受
        if($this->_ordersdf['pay_status'] != '1'){
            $this->_apiLog['info']['msg'] =  '未支付建行订单不接收';
            return false;
        }
    }      

    /**
     * 
     *
     * @return void
     * @author
     **/
    protected function canUpdate(){
        $rs = parent::canUpdate();
    
        $orderModel =     app::get(self::_APP_NAME)->model('orders');
        $obj_refund_apply =     app::get(self::_APP_NAME)->model('refund_apply');
        #检查是否有未完成退款 (所有退款申请单状态，属于已拒绝，才可以接受本次更新)
        $refund_info = $obj_refund_apply->getlist('order_id,status',array('order_id'=>$this->_tgOrder['order_id'],'status|noequal'=>3));
        if(!empty($refund_info)){
            $this->_apiLog['info'][] = '返回值：系统已存在退款，不再更新';
            return false;
        }
        #全额退款，订单取消
        if ($this->_ordersdf['status'] == 'dead') { 
            #订单未发货
            if ($this->_tgOrder['ship_status'] == '0' && $this->_tgOrder['status'] == 'active') {
                $ordersdf = array('payed' => '0','pay_status' => '5');
                $orderModel->update($ordersdf,array('order_id' => $this->_tgOrder['order_id']));
                $memo = '前端已退款，取消订单';
                $orderModel->cancel($this->_tgOrder['order_id'],$memo,false,'async');
                $this->_apiLog['info'][] = '返回值：订单退款取消成功';
            } else {
                $this->_apiLog['info'][] = '返回值：订单已发货或完成，不能更新取消！';
                return false;
            }
        } 
        return $rs;
    }   
}